<template>
  <div class="app-container">
    <div
      class="header"
      style="display: flex; justify-content: space-between"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item>
          <el-button
            icon="refresh"
            color=" rgb(69, 76, 105)"
            style="color: #fff"
            @click="resetQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleExport"
            color="rgb(146,226,206)"
            style="color: rgb(120, 120, 120); padding: 0 20px"
            v-hasPermi="['system:set:import']"
          >
            导 出
          </el-button>
        </el-form-item>
      </el-form>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item
          label=""
          prop="orderId"
        >
          <el-input
            v-model="queryParams.orderId"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="dialogueId"
        >
          <el-input
            v-model="queryParams.dialogueId"
            placeholder="请输入对话ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label=""
          prop="name"
        >
          <el-input
            v-model="queryParams.name"
            placeholder="请输入昵称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="发布时间" style="width: 308px;">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button
            type="info"
            icon="Search"
            @click="handleQuery()"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="loading"
      :data="dialogueList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="对话ID"
        align="center"
        prop="dialogueId"
      />
      <el-table-column
        label="昵称"
        align="center"
        prop="name"
      />
      <el-table-column
        label="手机号码"
        align="center"
        prop="phoneNumber"
      />
      <el-table-column
        label="发言内容"
        align="center"
        prop="content"
      />
      <el-table-column label="图片" align="center" prop="picture">
        <template #default="scope">
          <el-image style="width: 80px; height: 70px; display: block" v-if="scope.row.picture"
                    :src="scope.row.picture"
                    it="contain"
                    title="点击查看大图"
                    :preview-src-list="[scope.row.picture]"
                    preview-teleported
                    hide-on-click-modal>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template #default="scope">
          <span>{{
            parseTime(scope.row.publishTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单编号"
        align="center"
        prop="orderId"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dialogue:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话列对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="dialogueRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="订单ID"
          prop="orderId"
        >
          <el-input
            v-model="form.orderId"
            placeholder="请输入订单ID"
          />
        </el-form-item>
        <el-form-item
          label="发言人ID"
          prop="speakerId"
        >
          <el-input
            v-model="form.speakerId"
            placeholder="请输入发言人ID"
          />
        </el-form-item>
        <el-form-item
          label="发言人角色"
          prop="spearkerRole"
        >
          <el-input
            v-model="form.spearkerRole"
            placeholder="请输入发言人角色"
          />
        </el-form-item>
        <el-form-item label="发言内容">
          <editor
            v-model="form.content"
            :min-height="192"
          />
        </el-form-item>
        <el-form-item
          label="图片"
          prop="picture"
        >
          <el-input
            v-model="form.picture"
            placeholder="请输入图片"
          />
        </el-form-item>
        <el-form-item
          label="发布时间"
          prop="publishTime"
        >
          <el-date-picker
            clearable
            v-model="form.publishTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择发布时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dialogue">
import {
  listDialogue,
  getDialogue,
  delDialogue,
  addDialogue,
  updateDialogue,
} from "@/api/system/dialogue";

const { proxy } = getCurrentInstance();

const dialogueList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dialogueId: null,
    orderId: null,
    publishTime: null,
    name: null,
    phoneNumber: null,
  },
  rules: {
    orderId: [{ required: true, message: "订单ID不能为空", trigger: "blur" }],
    speakerId: [
      { required: true, message: "发言人ID不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询对话列列表 */
function getList() {
  loading.value = true;
  listDialogue(proxy.addDateRange(queryParams.value,dateRange.value)).then((response) => {
    dialogueList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    dialogueId: null,
    orderId: null,
    speakerId: null,
    spearkerRole: null,
    content: null,
    picture: null,
    publishTime: null,
  };
  proxy.resetForm("dialogueRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.dialogueId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加对话列";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _dialogueId = row.dialogueId || ids.value;
  getDialogue(_dialogueId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改对话列";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dialogueRef"].validate((valid) => {
    if (valid) {
      if (form.value.dialogueId != null) {
        updateDialogue(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDialogue(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _dialogueIds = row.dialogueId || ids.value;
  proxy.$modal
    .confirm('是否确认删除对话列编号为"' + _dialogueIds + '"的数据项？')
    .then(function () {
      return delDialogue(_dialogueIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/dialogue/export",
    {
      ...queryParams.value,
    },
    `dialogue_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
